package ipdesk.model.report.invade.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ipdesk.model.report.invade.bean.InvadeInfoBean;
import ipdesk.model.report.seminar.bean.SeminarInfoBean;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.opensymphony.xwork2.ActionContext;

import ecg.util.SessionUtils;
import ecg.util.SqlMapLocator;

public class InvadeInfoImpl extends SqlMapDaoTemplate implements IInvadeInfo
{
	
	public InvadeInfoImpl(DaoManager manager)
	{
		super(manager);
	}
	
	public int invadeInfoReg(InvadeInfoBean invadeInfoBean) throws Exception
	{
		int invadeCodeCount = 0;
		int lastInvadeSeq = 0;
		String invadeCode = "";
		
		
		String consultingTime = invadeInfoBean.getConsultingDate();
		String deskCode = invadeInfoBean.getDeskCode();
		
		// 관리코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		invadeCodeCount = ((Integer)this.queryForObject("invade.invadeCode-count", invadeInfoBean)).intValue();
				
		String endNumber = StringUtils.leftPad(Integer.toString(invadeCodeCount+1), 4, "0");
		
		// 생성된 침해조사일지 관리코드.
		invadeCode = consultingTime.replaceAll("-","") + "-" + deskCode.substring(4) + "-INF-" + endNumber;
		
		
		
		// 생성된 침해조사일지 관리코드 빈 객체에 저장.
		invadeInfoBean.setManageNo(invadeCode);
		invadeInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// iBatis 연동. 데이터 insert.
		lastInvadeSeq = ((Integer)this.insert("invade.invadeInfoInsert", invadeInfoBean)).intValue();
		
		return lastInvadeSeq;
	}
	
	public void insertFileData(InvadeInfoBean invadeInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("invade.invadeFileInfoInsert", invadeInfoBean);
	}
	
	public int getDataTotalCount(InvadeInfoBean invadeInfoBean) throws Exception
	{
		
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		
		//if(invadeInfoBean.getDeskCode() == null || "".equals(invadeInfoBean.getDeskCode())){
		//	map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		//}else{
			map.put("deskCode", invadeInfoBean.getDeskCode());
		//}
		
		if((invadeInfoBean.getInvadeSearchKeyword()) != null && (!invadeInfoBean.getInvadeSearchKeyword().equals("")))
		{
			map.put("invadeSearchOption", invadeInfoBean.getInvadeSearchOption());
			map.put("invadeSearchKeyword", invadeInfoBean.getInvadeSearchKeyword());
		}
		
		if(invadeInfoBean.getYear() != null && (!invadeInfoBean.getYear().equals(""))) {
			map.put("year", invadeInfoBean.getYear());
		}

		int totalDataCount = ((Integer)this.queryForObject("invade.invadeInfoTotalcount", map)).intValue();

		return totalDataCount;
	}
	
	public List<InvadeInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		System.out.println(map.get("startRow"));
		System.out.println(map.get("limitRowCount"));
		List<InvadeInfoBean> pageDataList = this.queryForList("invade.invadePageDataListForUser", map);
		
		return pageDataList;
	}
	
	public InvadeInfoBean getDetailData(int invadeSeq)
	{
		InvadeInfoBean invadeInfoBean = (InvadeInfoBean)this.queryForObject("invade.invadeDetailData", invadeSeq);
		
		return invadeInfoBean;
		
	}
	
	public void invadeInfoUpdate(InvadeInfoBean invadeInfoBean)
	{
		System.out.println("***수정메소드3 들어옴");
		this.update("invade.invadeInfoUpdate", invadeInfoBean);
	}
	
	public void insertFileData2(InvadeInfoBean invadeInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("invade.invadeFileInfoInsert2", invadeInfoBean);
	}
	
	public void delFileData(String fileSeq)
	{
		this.delete("invade.invadeDelFileData", fileSeq);
	}
	
	public void delDetailData(int invadeSeq)
	{
		this.update("invade.invadeDelDetailData", invadeSeq);
	}

	public void insertCompleteData(InvadeInfoBean invadeInfoBean) {
		// TODO Auto-generated method stub
		this.update("invade.invadeCompleteInsert", invadeInfoBean);
	}

	public InvadeInfoBean getFileDetailData(InvadeInfoBean invadeInfoBean) {
		// TODO Auto-generated method stub
		return (InvadeInfoBean)this.queryForObject("invade.fileDetailData", invadeInfoBean);
	}

	public void insertAdminData(InvadeInfoBean invadeInfoBean) {
		// TODO Auto-generated method stub
		this.update("invade.invadeAdminInsert", invadeInfoBean);
	}
}
